package com.where_to_play_search;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;

import com.example.where_to_play.R;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.where_to_play.MainActivity;
import com.where_to_play.adapter.Search_listview_Adapter;
import com.where_to_play.util.FragmentChangeHelper;

public class Search_Fragment extends Fragment {
	private String filename = "search_network_data";
	private int pageNum = 0;
	private MainActivity mactivity;
	private FragmentChangeHelper helper;
	private List<Map<String, String>> listtotal = new ArrayList<Map<String, String>>();
	private AssetManager assetmanager;
	@ViewInject(R.id.listview_search)
	private ListView listview;
	private Search_listview_Adapter adapter;
	private boolean isbottom = false;
	private boolean istop = false;

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		this.mactivity = (MainActivity) activity;
		assetmanager = mactivity.getAssets();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mactivity.changeFragment(helper);
		loadnetworkData();
	}

	private void loadnetworkData() {
		try {
			InputStream is = assetmanager.open(filename + pageNum + ".txt");
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			byte buf[] = new byte[1024 * 8];
			int len;
			while ((len = is.read(buf)) != -1) {
				baos.write(buf, 0, len);
			}
			String str = new String(baos.toByteArray());
			listtotal = GetlistFromSearch.getlistfromsearch(str);
			Log.i("listtotal",
					"===============================>" + listtotal.toString());

			/*
			 * Log.i("imgurl", "===============================>" +
			 * listtotal.get(0).get("imgUrl"));
			 */

			Log.i("str", "---------------------------->" + str);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.search__fragment, container,
				false);
		ViewUtils.inject(this, view);
		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		adapter = new Search_listview_Adapter(listtotal, mactivity);
		listview.setAdapter(adapter);
		listview.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				// TODO Auto-generated method stub
				if (isbottom && scrollState == 0) {
					pageNum++;
					loadnetworkData();
					if (pageNum < 5) {
						adapter.reloadListView(listtotal, true);
					}
				}
				if (istop && scrollState == 0) {
					pageNum = 0;
					loadnetworkData();
					adapter.reloadListView(listtotal, true);
				}
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				// TODO Auto-generated method stub
				if (firstVisibleItem + visibleItemCount == totalItemCount) {
					isbottom = true;
				} else {
					isbottom = false;
				}
				if (firstVisibleItem == 0) {
					istop = true;
				} else {
					istop = false;
				}

			}
		});
	}
}
